package com.ct.gd.passport.domain;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;

import com.fuckhtc.gson.Gson;
import com.up91.common.android.connect.APNType;
import com.up91.common.android.connect.NetUtil;

public class ExchangeRateInfo {
	
	static ArrayList<EXCHANGE_TYPE[]> trans = new ArrayList<EXCHANGE_TYPE[]>();
	static {
		trans.add(new EXCHANGE_TYPE[]{EXCHANGE_TYPE.RMB,EXCHANGE_TYPE.HKG});
		trans.add(new EXCHANGE_TYPE[]{EXCHANGE_TYPE.HKG,EXCHANGE_TYPE.RMB});
		trans.add(new EXCHANGE_TYPE[]{EXCHANGE_TYPE.RMB,EXCHANGE_TYPE.USD});
		trans.add(new EXCHANGE_TYPE[]{EXCHANGE_TYPE.USD,EXCHANGE_TYPE.RMB});
		trans.add(new EXCHANGE_TYPE[]{EXCHANGE_TYPE.HKG,EXCHANGE_TYPE.USD});
		trans.add(new EXCHANGE_TYPE[]{EXCHANGE_TYPE.USD,EXCHANGE_TYPE.HKG});
//		trans.add(new EXCHANGE_TYPE[]{EXCHANGE_TYPE.RMB,EXCHANGE_TYPE.MAC});
//		trans.add(new EXCHANGE_TYPE[]{EXCHANGE_TYPE.MAC,EXCHANGE_TYPE.RMB});
	}
	
	public ExchangeRateInfo(String lhs, String rhs) {
		super();
		this.lhs = lhs;
		this.rhs = rhs;
	}

	String lhs;
	String rhs;
	String error;
	
	public String getLhs() {
		return lhs;
	}

	public void setLhs(String lhs) {
		this.lhs = lhs;
	}

	public String getRhs() {
		return rhs;
	}

	public void setRhs(String rhs) {
		this.rhs = rhs;
	}
	
	public String getError() {
		return error;
	}

	public void setError(String error) {
		this.error = error;
	}

	@Override
	public String toString() {
		return "ExchangeRateInfo [lhs=" + lhs + ", rhs=" + rhs + ", error="
				+ error + "]";
	}

	public static List<ExchangeRateInfo> load(){
		List<ExchangeRateInfo>  result = new ArrayList<ExchangeRateInfo>();
		for(EXCHANGE_TYPE[] types : trans){
			EXCHANGE_TYPE key = types[0];
			EXCHANGE_TYPE val = types[1];
			ExchangeRateInfo info = getRate(key, val);
			System.out.println(info.toString());
			info.error = "兑换";
			result.add(info);
		}
		return result;
	}

	private static ExchangeRateInfo getRate(EXCHANGE_TYPE lhs,EXCHANGE_TYPE rhs) {
		try {
			DefaultHttpClient httpClient = new DefaultHttpClient();
			APNType apn = NetUtil.getCunnAPN();
			if (null != apn.getIp()) {
				HttpHost proxy = new HttpHost(apn.getIp(), apn.getPort());
				httpClient.getParams().setParameter(
						ConnRoutePNames.DEFAULT_PROXY, proxy);
			}
			HttpUriRequest request = new HttpGet(getUrl(lhs.hs, rhs.hs));
			BufferedReader br = null;
			HttpResponse response = null;
			StringBuilder sb = null;

			response = httpClient.execute(request);
			HttpEntity resEntity = response.getEntity();
			InputStream is = resEntity.getContent();
			br = new BufferedReader(new InputStreamReader(is));
			sb = new StringBuilder();
			String line;
			while ((line = br.readLine()) != null) {
				sb.append(line);
			}
			ExchangeRateInfo info =  new Gson().fromJson(new String(sb.toString().getBytes(),"UTF-8"), ExchangeRateInfo.class);
			if(StringUtils.isBlank(info.error)){
				String lhsVal = "1"+lhs.name;
				String rhsVal = info.rhs.substring(0,info.rhs.indexOf(" ")).substring(0, 5)+rhs.name;
				info.lhs =lhsVal;
				info.rhs = rhsVal;
			}
			return info;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new ExchangeRateInfo(lhs.name,rhs.name);
	}
	
	private static String getUrl(String lhs,String rhs){
		return "http://www.google.com/ig/calculator?hl=en&q=1" +lhs +
				"%3D%3F" +rhs;
	}
}

enum EXCHANGE_TYPE{
	RMB("RMB","人民币"),
	HKG("HKD","港币"),
	USD("USD","美元"),
	MAC("MOP","澳元");
	
	String hs;
	String name;
	private EXCHANGE_TYPE(String hs, String name) {
		this.hs = hs;
		this.name = name;
	}
	
	public static String getHsName(String hs){
		for (EXCHANGE_TYPE type :EXCHANGE_TYPE.values()){
			if(type.hs.equals(hs)){
				return type.name;
			}
		}
		return RMB.name;
	}
}